﻿@using KMF.Common
@using KMF.Utility
@using KMF.WebUI.CustomHelpers
@{
    var parentTemplateList = (List<VCProCateTempMappingDto>)ViewBag.ParentTemplateList;
    var subTemplateList = (List<SubCategoryTemplateDto>)ViewBag.SubTemplateList;
    var productList = (Dictionary<string, List<ProductRateByCateTopListPagingDto>>)ViewBag.ProductList;
    var bannerList = (List<ViewBannerClientDto>)ViewBag.BannerByCateList;
    
    var htmlTag = string.Empty;
}

@if (parentTemplateList.Any() && subTemplateList.Any())
{
     foreach (var pItem in parentTemplateList)
     {
         var sItemList = subTemplateList.Where(m => m.ProductCateTempMappingId == pItem.Id).ToList();
         var pItemList = productList[pItem.ProductCateId];
         if (sItemList.Count == 4 && pItemList.Count == 4)
         {
             var arrString = new string[4];
             for (var i = 0; i < 4; i++)
             {
                 var StarPercent = "0";
                 if (pItemList[i].AverageRate > 0)
                 {
                     //Star
                     StarPercent = (Convert.ToDouble(pItemList[i].AverageRate) * 100 / 5).ToString("#.#").Replace(',', '.');
                 }
                 arrString[i] = string.Format(sItemList[i].TempContent, StarPercent,
                                        Url.Action("Index", "ProductDetail", new { productId = pItemList[i].Id, productName = StringUtil.RemoveSpecialCharactersVn(pItemList[i].Name) }),
                                        pItemList[i].Name,
                                        pItemList[i].PromotionPrice > 0 ? pItemList[i].Price.ToString("#,###,###") + " <span>đ</span>" : "",
                                        pItemList[i].PromotionPrice > 0 ? pItemList[i].PromotionPrice.ToString("#,###,###") + " <span>đ</span>" : pItemList[i].Price.ToString("#,###,###") + " <span>đ</span>",
                                        Url.Action("Index", "ProductDetail", new { productId = pItemList[i].Id, productName = StringUtil.RemoveSpecialCharactersVn(pItemList[i].Name) }),
                                        pItemList[i].Name,
                                        pItemList[i].ImagePath.ImagePathThumbDefault(),
                                        ""
                                        );
             }
             
             var bannerbyCate = bannerList.Where(m => m.PageKey == "TRANG-CHU" & m.PositionKey == "BANNER-SAN-PHAM-THEO-DANH-MUC" & m.CategoryId == pItem.ProductCateId);
             var banner = new ViewBannerClientDto();
             if(bannerbyCate.Any()){
                 banner = bannerbyCate.First();
             }

             var urlProductByCate = CommonHelpers.GenerateUrlProductByCate(pItem.CateName, pItem.ProductCateId, pItem.Depth);
             
             var pStr = string.Format(pItem.TempContent,
                                    urlProductByCate,
                                    pItem.CateName, pItem.ProductCateId,
                                    arrString[0], arrString[1], arrString[2], arrString[3],
                                    banner.Url, banner.Description, banner.ImagePath.ImagePathThumbDefault()
                                    );
             
             htmlTag += pStr;
         }
         
         
         
     }
    
    @Html.Raw(htmlTag);
}